package com.example.md.mddemo.java8.stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Copyright © 2018年 mddemo. All rights reserved.
 *
 * @author 临江仙 hzqiuxm@163.com
 * 使用迭代的方式去获取想要的水果信息
 * @date 2018/12/1 11:08
 * @Since 1.0.0
 */
public class IteratorFruitSelector extends FruitSelector {
    @Override
    protected List<Double> doFilter(List<Fruit> fruits) {

        //get the fruit name is 'apple'
        List<Fruit> appleList = new ArrayList<>();
        Iterator<Fruit> iterator = fruits.iterator();

        while (iterator.hasNext()){

            Fruit fruit = iterator.next();

            if(fruit.getName().equals(CANDIDATE_FRUIT)){
                appleList.add(fruit);
            }

        }

        //do sort 降序
        Collections.sort(appleList,(c1,c2)->{
            if(c1.getPrice() > c2.getPrice()) {return 1;}
            else if(c1.getPrice() == c2.getPrice()) {return 0;}
            else {return -1;}
        });

        //do filter
        List<Double> applePriceList = new ArrayList<>();
        for (Fruit fruit : appleList){
            applePriceList.add(fruit.getPrice());
        }

        return applePriceList;
    }
}
